gusucode.com > VC++ 读取Intel CPU序列号 > VC++ 读取Intel CPU序列号/gusucode/IntelCPUIDDlg.cpp

    //Download by http://www.NewXing.com
// IntelCPUIDDlg.cpp : implementation file
//

#include "stdafx.h"
#include "IntelCPUID.h"
#include "IntelCPUIDDlg.h"
//#include <stdio.h>
//#include <conio.h>


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CIntelCPUIDDlg dialog

CIntelCPUIDDlg::CIntelCPUIDDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CIntelCPUIDDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CIntelCPUIDDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CIntelCPUIDDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CIntelCPUIDDlg)
	DDX_Control(pDX, IDC_EDIT_VENDOR, m_editVendor);
	DDX_Control(pDX, IDC_EDIT_CPUID, m_editCPUID);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CIntelCPUIDDlg, CDialog)
	//{{AFX_MSG_MAP(CIntelCPUIDDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_CPUID, OnBtnCPUID)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CIntelCPUIDDlg message handlers

BOOL CIntelCPUIDDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CIntelCPUIDDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CIntelCPUIDDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

// “获得Intel CPU ID”按钮消息处理函数
void CIntelCPUIDDlg::OnBtnCPUID() 
{
  unsigned long s1,s2;
  unsigned char vendor_id[]="------------";//CPU提供商ID
  CString str1,str2,str3;
  // 以下为获得CPU ID的汇编语言指令
  _asm    // 得到CPU提供商信息 
  {  
      xor eax,eax   // 将eax清0
      cpuid         // 获取CPUID的指令
      mov dword ptr vendor_id,ebx
      mov dword ptr vendor_id[+4],edx
      mov dword ptr vendor_id[+8],ecx  
  }
  str1.Format("%s",vendor_id);
 
  _asm    // 得到CPU ID的高32位 
  { 
	  mov eax,01h    
      xor edx,edx
      cpuid
      mov s2,eax
  }
  str2.Format("%08X-",s2);
  
  _asm    // 得到CPU ID的低64位
  { 
	  mov eax,03h
      xor ecx,ecx
      xor edx,edx
      cpuid
      mov s1,edx
      mov s2,ecx
  }
  str3.Format("%08X-%08X\n",s1,s2);

  str2=str2+str3;
  m_editVendor.SetWindowText(str1);
  m_editCPUID.SetWindowText(str2);	
}